<?php
namespace App\Services;
use GuzzleHttp\Client;
// use App\Models\Linkslistsmedias;
// use App\Models\Users;
// use App\Models\Usersinvitations;
use Illuminate\Support\Facades\Storage;
use App\Services\WechatopenService;
use Illuminate\Support\Facades\Redis;
use Log,Exception,Config;

class WechatsService
{
    public static function code2Session($js_code,$appid,$secret)
    {
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$js_code.'&grant_type=authorization_code');
        $body = $response->getBody();
        return json_decode($body,true);
    }

    // 启动ticket推送服务
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/component_verify_ticket_service.html
    public static function apiStartPushTicket($component_appid,$component_secret)
    {
        $client = new Client;
        $params = ["component_appid"=>$component_appid,"component_secret"=>$component_secret];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_start_push_ticket', [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        if ($jsonbody["errcode"] == 0) {
            if ($jsonbody["errmsg"] == "in a normal state") return ["status"=>"1001","msg"=>"现在状态已正常开启,无需重复开启"];
            return ["status"=>"1000","msg"=>"ticket推送服务开启成功！"];
        } else {
            if ($jsonbody["errcode"] == 41018) return ["status"=>"1002","msg"=>"Appid为空"];
            if ($jsonbody["errcode"] == 41004) return ["status"=>"1003","msg"=>"Appsecret为空"];
            return ["status"=>"1004","msg"=>"ticket推送服务开启失败！"];
        }
    }

    // 获取令牌
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/component_access_token.html
    public static function apiComponentToken($component_appid,$component_appsecret,$component_verify_ticket)
    {
        $client = new Client;
        $params = ["component_appid"=>$component_appid,"component_appsecret"=>$component_appsecret,"component_verify_ticket"=>$component_verify_ticket];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_component_token', [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        return json_decode($body,true);
    }

    // 获取预授权码
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/pre_auth_code.html
    public static function apiCreatePreauthcode()
    {
        $config = Config::get("wechats.open_platform.beianbao");
        $component_appid = $config["app_id"];
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $client = new Client;
        $params = ["component_appid"=>$component_appid];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        return json_decode($body,true);
    }

    // 使用授权码获取授权信息
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/authorization_info.html
    public static function apiQueryAuth($authorization_code)
    {
        $config = Config::get("wechats.open_platform.beianbao");
        $component_appid = $config["app_id"];
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $client = new Client;
        $params = ["component_appid"=>$component_appid,"authorization_code"=>$authorization_code];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        return json_decode($body,true);
    }

    // 获取授权方的帐号基本信息
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/api_get_authorizer_info.html
    public static function apiGetAuthorizerInfo($authorizer_appid)
    {
        $config = Config::get("wechats.open_platform.beianbao");
        $component_appid = $config["app_id"];
        $component_access_token = WechatopenService::getComponentAccessToken($component_appid);
        $client = new Client;
        $params = ["component_appid"=>$component_appid,"authorizer_appid"=>$authorizer_appid];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        return json_decode($body,true);
    }

    // 快速注册个人小程序
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Register_Mini_Programs/fastregisterpersonalweapp.html
    public static function fastRegisterPersonalWeapp($component_access_token,$idname,$wxuser,$component_phone)
    {
        $client = new Client;
        $params = ["idname"=>$idname,"wxuser"=>$wxuser,"component_phone"=>$component_phone];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/component/fastregisterpersonalweapp?action=create&component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        return json_decode($body,true);
    }

    public static function getvisitstatus($access_token)
    {
        $client = new Client;
        $params = [];
        $response = $client->request('POST', 'https://api.weixin.qq.com/wxa/getvisitstatus?access_token='.$access_token, [
            'body' => '{}',
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        return json_decode($body,true);
    }
}
